class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
        int m = mat.size();
        int n = mat[0].size();
        if(m*n != r*c ||(m == r && n==c)) return mat;
        else{
            vector<vector<int>> res(r, vector<int>(c,0));
            for(int i = 0; i < m*n; ++i){
                res[i/c][i%c] = mat[i/n][i%n];
            }
            return res;
        }
    }
};
